package com.flyee.viewer.pack.helper
{	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;

	/**
	 * 包裹的逻辑处理类，主要是负责和服务器数据交换
	 * @author dfengwji
	 *
	 */
	public class PackageHelper
	{
		public static var AVERAGE_COUNT:int = 0;
		public static var TOTAL_COUNT:int = 0;
		//private var packageService:UserPackageFlexService = null;
		//private var user:UserModel = null;
		private var packContainer:PackageContainer = null;
		private var adaptItemPosFun:Function = null;
		private var userId:String = null;
		public function PackageHelper(root:PackageContainer)
		{
			packContainer = root;
			//user = ApplicationContext.getInstance().sessionUser;
			//userId = ApplicationContext.getInstance().sessionUser.id;
			//packageService = new UserPackageFlexService();
			//packageService.addEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			//packageService.addEventListener(UserPackageFlexService.GETITMEINPACKAGE_FUN,itemListResultEvent);
			//packageService.addEventListener(UserPackageFlexService.REMOVE_ITEM_FUN,removeItemResultEvent);
			getItemInPackage();
			//packContainer.setGoldCoin(user.goldCoin);
			//packContainer.setBlueCoin(user.cityCoin);
		}
		
		public function getItemInPackage():void{
			//packageService.getItemInPackage(userId);
		}
		
		public function removeItem(itemId:String,position:int,count:int):void{
			//packageService.removeItem(userId,itemId,position,count);
		}
		
		public function clearAll():void{
			//packageService.removeEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			//packageService.removeEventListener(UserPackageFlexService.GETITMEINPACKAGE_FUN,itemListResultEvent);
			//packageService.removeEventListener(UserPackageFlexService.REMOVE_ITEM_FUN,removeItemResultEvent);
			//packageService.clearAll();
			//packageService = null;
			packContainer = null;
		}
		/**
		 * 删除道具的结果处理事件 
		 * @param event
		 * 
		 */		
		private function removeItemResultEvent(event:ResultEvent):void{
			if(event.result){
				var itemData:ItemDataModel = event.result as ItemDataModel;
				if(itemData.result){
					packContainer.destroyItems(itemData);
				}else{
					Alert.show(itemData.message,"异常");
				}
			}else{
				Alert.show("移除道具数据出现异常！","异常");
				//UIManager.getInstance().removedFromDesktopByName(PackagePanel.NAME);
			}
		}
		
		private function itemListResultEvent(event:ResultEvent):void{
			if(event.result){
				var items:ArrayCollection = event.result as ArrayCollection;
				var leng:int = items.length;
				for(var i:int = 0;i < leng;i++){
					var itemData:ItemDataModel = items.getItemAt(i) as ItemDataModel;
					var item:ItemViewModel = ItemHelper.cloneProp(itemData,false);
					item.loadIcon();
					item.toolTip = item.dataModel.tip;
					packContainer.addOneProp(item);
				}
			}else{
				Alert.show("初始化包裹数据出现异常！","异常");
				//UIManager.getInstance().removedFromDesktopByName(PackagePanel.NAME);
			}
		}
		
		/* private function exceptionEvent(event:RemoteExceptionEvent):void{
			Alert.show(event.message,"异常");
			UIManager.getInstance().removedFromDesktopByName(PackagePanel.NAME);
		} */
	}
}